const fs = require('fs')
const path = require('path')
const compressing = require('compressing');


const tempFolder = path.resolve(__dirname, './dist/plugin_development')
if (fs.existsSync(tempFolder) === false) {
    fs.mkdirSync(tempFolder, { recursive: true })
}
const len = __dirname.length

function build(folder) {
    fs.readdirSync(folder).forEach(file => {
        const filePath = path.resolve(folder, file)
        if (['node_modules', 'dist', 'buildDev.js', '.git', '.vscode', '.vs'].indexOf(file) > -1) {
            return
        }
        let newPath = tempFolder + filePath.slice(len)
        if (fs.statSync(filePath).isDirectory()) {
            if (fs.existsSync(newPath) === false) {
                fs.mkdirSync(newPath, { recursive: true })
            }
            build(filePath)
        } else {
            fs.copyFileSync(filePath, newPath)
        }
    })
}
function compress(source) {
    return new Promise((resolve, reject) => {
        const destination = path.resolve(__dirname, `./dist/poluginDevelopment.zip`);
        if (fs.existsSync(destination)) {
            fs.unlinkSync(destination)
        }
        compressing.zip.compressDir(source, destination)
            .then(() => {
                console.log('压缩完成。');
                resolve('压缩完成')
            })
            .catch(err => {
                console.error('压缩失败：', err);
                reject('压缩失败：', err)

            });
    })

}
build(__dirname)
compress(tempFolder).then(resp => {
    fs.rmSync(tempFolder, { recursive: true })
})
console.log(tempFolder)

